package com.example.computertms.controller;

import com.example.computertms.pojo.ComputerType;
import com.example.computertms.pojo.Users;
import com.example.computertms.servers.ComputerTypeServer;
import com.example.computertms.servers.UserServers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MiniAPI {
    @Autowired
    UserServers userServers;
    @Autowired
    ComputerTypeServer computerTypeServer;

    @RequestMapping("/getOpenid")
    public String getOpenid(@RequestParam String code,@RequestParam String from) {
        String url="https://api.weixin.qq.com/sns/jscode2session?appid="+
                from+"&secret=3d703dcae637340ea43133a8d3e1846c&js_code="+
                code+"&grant_type=authorization_code";
        RestTemplate rt = new RestTemplate();
        String resp = rt.getForObject(url,String.class);
        return resp;
    }

    @RequestMapping("/register")
    public Map<String, Object> register(Users users){
        Map<String,Object> map = new HashMap<>();
        if (userServers.findUserByAccount(users.getAccount()) != null){
            Users user =  userServers.findUserByAccount(users.getAccount());
            if(user.getAccount().equals(users.getAccount())){
                map.put("success","true");
                map.put("isRegister","false");
                map.put("errCode","001");
                map.put("msg","the account already exists! ");
            }else{
                userServers.addUser(users);
                map.put("success","true");
                map.put("isRegister","true");
                map.put("msg","success");
                map.put("data",userServers.findUserByAccount(users.getAccount()));
            }
        }else{
            userServers.addUser(users);
            map.put("success","true");
            map.put("isRegister","true");
            map.put("msg","success");
            map.put("data",userServers.findUserByAccount(users.getAccount()));
        }
        return map;
    }

    @RequestMapping("/query")
    public Map<String,Object> query(@RequestParam String account){
        Map<String,Object> map = new HashMap<>();
        map.put("success","true");
        map.put("msg","success");
        map.put("data",userServers.findUserByAccount(account));
        if(userServers.findUserByAccount(account) != null){
            map.put("is_account",true);
        }else {
            map.put("is_account",false);
        }
        return map;
    }

    @RequestMapping("/changePassword")
    public Map<String,Object> changePassword(@RequestParam  String account,@RequestParam String password){
        userServers.changePassword(password,account);
        Map<String,Object> map = new HashMap<>();
        map.put("success","true");
        map.put("msg","success");
        return map;
    }


    @RequestMapping("/addComputerType")
    public Map<String,Object> addComputerType(ComputerType computerType){
        Map<String,Object> map = new HashMap<>();
        if(computerTypeServer.foundByComputerId(computerType.getComputerId()).size() != 0){
            ComputerType computer = computerTypeServer.foundByComputerId(computerType.getComputerId()).get(0);
            if (computer.getComputerId().equals(computerType.getComputerId())){
                map.put("success","true");
                map.put("errCode","0101");
                map.put("msg","the computerId already exists! ");
            }else {
                computerTypeServer.addComputerType(computerType);
                List<ComputerType> list =  computerTypeServer.foundAll();
                map.put("success","true");
                map.put("msg","success");
                map.put("data",list);
            }
        }else{
            computerTypeServer.addComputerType(computerType);
            List<ComputerType> list =  computerTypeServer.foundAll();
            map.put("success","true");
            map.put("msg","success");
            map.put("data",list);
        }
        return map;
    }

    @RequestMapping("/foundByComputerId")
    public Map<String,Object> foundByComputerId(@RequestParam String computerId){
        Map<String,Object> map = new HashMap<>();
        List<ComputerType> list =  computerTypeServer.foundByComputerId(computerId);
        map.put("success","true");
        map.put("msg","success");
        map.put("data",list);
        return map;
    }

    @RequestMapping("/foundByName")
    public Map<String,Object> foundByName(@RequestParam String name){
        Map<String,Object> map = new HashMap<>();
        List<ComputerType> list =  computerTypeServer.foundByName("%" + name + "%");
        map.put("success","true");
        map.put("msg","success");
        map.put("data",list);
        return map;
    }

    @RequestMapping("/changeType")
    public Map<String,Object> changeComputerTypeByComputerId(ComputerType computerType){
        computerTypeServer.changeComputerTypeByComputerId(computerType);
        Map<String,Object> map = new HashMap<>();
        List<ComputerType> list =  computerTypeServer.foundAll();
        map.put("success","true");
        map.put("msg","success");
        map.put("data",list);
        return map;
    }

    @RequestMapping("/deleteType")
    public Map<String,Object> deleteComputerTypeByComputerId(@RequestParam String computerId){
        computerTypeServer.deleteComputerTypeByComputerId(computerId);
        Map<String,Object> map = new HashMap<>();
        map.put("success","true");
        map.put("msg","success");
        map.put("data",computerTypeServer.foundAll());
        return map;
    }

    @RequestMapping("/foundAll")
    public List<ComputerType>  foundAll(){
        List<ComputerType> list = computerTypeServer.foundAll();
        return list;
    }
}
